<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="../../../attr.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!doctype html>
<html>
<meta http-equiv="X-UA-Compatible" content="IE=emulateIE7" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="${ctx}/css/sapar.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/css/common.css" />
<link href="${ctx}/css/select2.min.css" rel="stylesheet" />
<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/sapar.js"></script>
<script type="text/javascript" src="${ctx}/js/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/assets/layer/layer.js"></script>
<script type="text/javascript" src="${ctx}/js/kkpager.min.js"></script>
<link rel="stylesheet" type="text/css" href="${ctx}/css/kkpager_blue.css" />
<script type="text/javascript" src="${ctx}/js/jquery-1.10.1.js"></script>
<script type="text/javascript" src="${ctx}/js/mask.js"></script>
<title>临床科室包发放</title>
<style>
.kv-item select {
    width: 150px;
    height: 24px;
    border: solid gray 1px;
}
#yifa{
color:green;
font-weight:bold;
}
#weifa{
color:red;
font-weight:bold;
}

#chaxun{
margin-left:100px;
}
#mm{
    width:8em;
    margin-top: -2px;
}
#xiaoa{
float:left;
}
#xiala{
float:left;
}
th{
text-align: center;
}
td{
text-align: center;
}
</style>
</head>

<body>
<div id="mask" class="mask"></div>
<audio src="../../../audio/Beep_Short.mp3" id="audio"></audio><!-- 成功提示音 -->
<audio src="../../../audio/Alarm_Clock (1).mp3" id="audio1"></audio><!-- 失败提示音 -->
	<div id="saper-container">
		<div id="saper-hd"></div>
		<div id="saper-bd">
			<div class="subfiled clearfix">
				<h2>临床科室包发放</h2>
			</div>
			<div class="subfiled-content">
				<div class="search-box clearfix">
					<div class="kv-item clearfix">

						<label >包条码:</label>
						<div class="kv-item-content">
							<input type="text" name="packageTiaoma" placeholder="发放包条码"
								   class="ffr" id="packageTiaoma" >
						</div>

					</div>


					<div class="kv-item clearfix">

						<label id="mm">发放入:</label>
						<div class="kv-item-content">
							<input type="text" name="fafangren" placeholder="发放人条码"
								 class="ffr" id="fafangrentiaoma" >
						</div>

					</div>
					<div class="kv-item clearfix">
					<label>选择科室：</label>
					<select class="search_text" id="keshi">
						<c:if test="${department.departmentId != null }">
							<option value="${department.departmentId}">${department.departmentName}</option>
							<option value="0" >请选择</option>
						</c:if>
						<c:if test="${department.departmentId == null }">
							<option value="0">请选择</option>
						</c:if>
						<c:if test="${alldeparts != null}">
							<c:forEach items="${alldeparts}" var="pro">
								<c:if test="${pro.departmentId != 15 && pro.departmentId != department.departmentId }">
									<option value="${pro.departmentId}">${pro.departmentName}</option>
								</c:if>
							</c:forEach>
						</c:if>
					</select>
					</div>
					<a href="javascript:;" class="sapar-btn sapar-btn-recom query-btn" id="piliangfafang">发放</a>

					<a href="javascript:;" class="sapar-btn sapar-btn-recom query-btn" id="rukude">入库</a>
					<!-- <span class="anniu" id="rukude">入库</span> -->
				</div>
				<!--表格开始-->
				<div class="table">
					<%--<!--表格操作-->--%>
					<%--<div class="table-operate ue-clear" id="xiala">--%>
					<%--<c:if test="${danfeng != null }">--%>
					<%--<input type="checkbox"  name="tiaojian" value="0" class="xuan" id="dfb" checked="checked">单封包--%>
					<%--</c:if>--%>
					<%--<c:if test="${danfeng == null }">--%>
					<%--<input type="checkbox"  name="tiaojian" value="0" class="xuan" id="dfb">单封包--%>
					<%--</c:if>--%>
					<%--<c:if test="${tongyong != null }">--%>
					<%--<input type="checkbox"  name="tiaojian" value="0" class="xuan" id="tyb" checked="checked">通用包--%>
					<%--</c:if>--%>
					<%--<c:if test="${tongyong == null }">--%>
					<%--<input type="checkbox"  name="tiaojian" value="1" class="xuan" id="tyb">通用包--%>
					<%--</c:if>--%>

                        <%--<label>选择科室：</label>--%>
                            <%--<select class="search_text" id="keshi" >--%>
                                	    <%--<c:if test="${department.departmentId != null }">--%>
                                   	        <%--<option value="${department.departmentId}">${department.departmentName}</option>--%>
                                   	        <%--<option value="0" >请选择</option>--%>
                                   	    <%--</c:if>--%>
                                   	    <%--<c:if test="${department.departmentId == null }">--%>
                                   	        <%--<option value="0">请选择</option>--%>
                                   	    <%--</c:if>--%>
                             			<%--<c:if test="${alldeparts != null}">--%>
		                            	<%--<c:forEach items="${alldeparts}" var="pro">--%>
			                            	<%--<c:if test="${pro.departmentId != 15 && pro.departmentId != department.departmentId }">--%>
			                                	<%--<option value="${pro.departmentId}">${pro.departmentName}</option>--%>
			                                <%--</c:if>--%>
			                            <%--</c:forEach>--%>
	                                    <%--</c:if>--%>
                            <%--</select>--%>
 					<%--</div>--%>
 					<%--<div id="xiaoa"><a href="javascript:;" class="sapar-btn sapar-btn-recom query-btn" id="chaxunde">查询</a> </div>--%>
					<!--表格具体内容-->
					<div class="table-box">
						<table>
							<thead>
								<tr>
								  <th><input type="checkbox" id="checkall" name="checkall" /></th>
								  <th class="text-left">包条码</th>
			                      <th class="text-left">包名称</th>
			                      <th class="text-left">包类型</th>
			                      <th class="text-left">所属科室</th>
			                      <th class="text-left">合格时间</th>
			                      <th class="text-left">发放状态</th> 
								</tr>
							</thead>
							<tbody>
							    <c:if test="${shoushubaoList == null || shoushubaoList == '' }">
                                    <tr class="no-data product"><td colspan="10" style="color: red">暂时没有数据</td></tr>
                                </c:if>
								<c:if test="${shoushubaoList != null}">
									<c:forEach items="${shoushubaoList}" var="shoushubao">
											<tr>
											  <td><input type="checkbox" name="key" value="${shoushubao.packageTiaoma}" data2="${shoushubao.fafangLiushui}" data3="${shoushubao.baoRuku }"/></td>
											  <td>${shoushubao.packageTiaoma}</td>
						                      <td>${shoushubao.packageName}</td>
						                      <td>
						                        <c:if test="${shoushubao.tbPackageMuban.packageCategory ==0}">单封包</c:if>
						                        <c:if test="${shoushubao.tbPackageMuban.packageCategory ==1}">通用包</c:if>
						                        <c:if test="${shoushubao.tbPackageMuban.packageCategory ==null}">----</c:if>
						                      </td>
						                      <td>
						                      <c:if test="${shoushubao.tbPackageMuban.tbDepartment != null }">${shoushubao.tbPackageMuban.tbDepartment.departmentName}</c:if>
						                      <c:if test="${shoushubao.tbPackageMuban.tbDepartment == null}">----</c:if>
						                      </td>
						                      <td><fmt:formatDate value="${shoushubao.miejunShenheTime}" type="date" pattern="yyyy-MM-dd HH:mm:ss"/></td>
						                      <td>
						                      	<c:if test="${shoushubao.fafangLiushui==null && shoushubao.tbPackageMuban.packageCategory ==0}"><span id="weifa">未发放</span></c:if>
						                      	<c:if test="${shoushubao.baoRuku!=1 && shoushubao.tbPackageMuban.packageCategory ==1}"><span id="weifa">未入库</span></c:if>
						                      	<c:if test="${shoushubao.baoRuku!=1 && shoushubao.tbPackageMuban.packageCategory ==null}"><span id="weifa">----</span></c:if>
						                      </td>
						                    </tr>
									</c:forEach>
								</c:if>
							</tbody>
						</table>
					</div>
				</div>
				<!--表格结束-->
			</div>
		</div>
		<!--分页用到的 -->
		<%--<div id="saper-ft">--%>
			<%--<div id="kkpager"></div>--%>
		<%--</div>--%>
	</div>
</body>
<script src="/js/select2.js" type = "text/javascript" ></script>
<script src="/js/pinyin.js" type = "text/javascript" ></script>
<script type="text/javascript">
$(function(){
	var zhi=$("#keshi").val();
	var baolx;
	var dfb="";
	var tyb="";
	if($("#dfb").prop('checked')){
		dfb=$("#dfb").val();
	}
	if($("#tyb").prop('checked')){
		tyb=$("#tyb").val();
	}
	 var totalPage = ${total};
	var pageNo = getParameter('pageIndex');
	if(!pageNo){
		pageNo = 1;
	}
	//生成分页
	//有些参数是可选的，比如lang，若不传有默认值
	kkpager.generPageHtml({
		pno : pageNo,
		//总页码
		total : totalPage,
		//链接前部
		hrefFormer : '${ctx}/wupingfafang/linchuangbaoList',
		//链接尾部
		hrefLatter : '.do',
		//这些参数传过去干嘛的
		getLink : function(n){
			return this.hrefFormer + this.hrefLatter + "?pageIndex="+n+"&searchTxt="+$(".search_text").val()+"&keshi="+zhi+"&danfeng="+dfb+"&tongyong="+tyb;
		}
	});

    $('select').select2();
		
	<%--$("#chaxunde").click(function(){--%>
		<%--/* var dfb=$("#dfb").val();--%>
		<%--var tyb=$("#tyb").val(); */--%>
		<%--dfb=$("#dfb").val();--%>
		<%--tyb=$("#tyb").val();--%>
		<%----%>
		<%--zhi=$("#keshi").val();--%>
		<%--var ffrtm=$("#fafangrentiaoma").val();--%>
		<%--/* if(isNullOrEmpty(ffrtm)){--%>
	    	<%--layer.alert("请输入发放人条码");--%>
	    <%--} */--%>
		<%--if($("#dfb").prop('checked')&&$("#tyb").prop('checked')){--%>
		    <%--if(zhi==0){--%>
		    	<%--window.location.href="${ctx}/wupingfafang/chaxun.do?danfeng="+dfb+"&tongyong="+tyb;--%>
		    <%--}--%>
		    <%--else{--%>
		    	<%--window.location.href="${ctx}/wupingfafang/chaxun.do?danfeng="+dfb+"&tongyong="+tyb+"&keshi="+zhi;--%>
		    <%--}--%>
		<%--}else if($("#dfb").prop('checked')==false&&$("#tyb").prop('checked')==false){--%>
			<%--if(zhi==0){--%>
		    	<%--window.location.href="${ctx}/wupingfafang/chaxun.do";--%>
		    <%--}--%>
		    <%--else{--%>
		    	<%--window.location.href="${ctx}/wupingfafang/chaxun.do?keshi="+zhi;--%>
		    <%--}--%>
		<%--}else{--%>
			<%--if(zhi==0){--%>
				<%--if($("#dfb").prop('checked')){--%>
					<%--window.location.href="${ctx}/wupingfafang/chaxun.do?danfeng="+dfb;--%>
					<%--$("#dfb").prop("checked",true);--%>
				<%--}--%>
				<%--if($("#tyb").prop('checked')){--%>
					<%--window.location.href="${ctx}/wupingfafang/chaxun.do?tongyong="+tyb;--%>
				<%--}--%>
			<%--}--%>
			<%--else{--%>
				<%--if($("#dfb").prop('checked')){--%>
					<%--window.location.href="${ctx}/wupingfafang/chaxun.do?danfeng="+dfb+"&keshi="+zhi;--%>
				<%--}--%>
				<%--if($("#tyb").prop('checked')){--%>
					<%--window.location.href="${ctx}/wupingfafang/chaxun.do?tongyong="+tyb+"&keshi="+zhi;--%>
				<%--}--%>
			<%--}--%>
		<%--}--%>
	<%--});--%>
	
	//全选
	$("#checkall").click(function(){
	   if(this.checked){
	        $("input[name='key']").prop('checked', true)
	   }else{
	        $("input[name='key']").prop('checked', false)
	   }
	});
	
	<%--发放的页面事件（点击确认发放）--%>
	$("#piliangfafang").click(function(){
		var btn = true;
		debugger
		var tiaomas=getSelRecord();//复选框选中的包
		var ffrtiaoma=$("#fafangrentiaoma").val();
		var fafangzhuangtai=getfafangzhuangtai();
		//判断发放人条码是否为空
		if(isNullOrEmpty(ffrtiaoma)){
			alert("请输入发放人条码");
			btn=false;
			return btn;
		}
		var fafangkeshi=$("#keshi").val();
		
        //判断是否选择了包
		if(tiaomas.length==0){
			alert("请先选择包");
			btn = false;
			return btn;
		}
        if(fafangkeshi==0){
            alert("请先选择科室");
            btn = false;
            return btn;
        }

		$('input[name="key"]:checked').each(function(){
			if(isNullOrEmpty($(this).attr("data2"))){
			    btn = true;
			    return btn;
			}else{
				alert("不能选择已发放的包");
				btn = false;
				return btn;
			}
		});
	    if(btn){
	    	showMask();
	    	//弹出显示包条码
	    	$.ajax({
	            type: "POST",
	            url:"${ctx}/wupingfafang/judgebao.do?fafangkeshi="+fafangkeshi+"&fafangren="+ffrtiaoma,
				data:{"tiaomas":JSON.stringify(tiaomas)},
	            error: function(request) {
	           	 	layer.alert("网络故障，请稍后再试!");
	            },
	            success: function(data) {
	            	$("#mask").hide();
		           	if(data.status=="true"){
		 	           	layer.alert(data.msg, function(){
		 	           		//弹出显示选中包条码的弹出层
		 	           		window.location.href="${ctx}/wupingfafang/linchuangbaoList.do";
	          		    });
		           	}else{
		           		layer.msg(data.msg);
		           	}
	            }
	        });
	    }	
	});
	
	$("#rukude").click(function(){
		var btn = true;
		debugger

		var tiaomas=getSelRecord();//复选框选中的包
		//没有入库人这个字段
		var rkrtiaoma=$("#fafangrentiaoma").val();
		var rukuzhuangtai=getrukuzhuangtai();
		var fafangkeshi=$("#fafangkeshi").val();
		if(tiaomas.length==0){
			alert("请先选择包");
			btn = false;
			return btn;
		}
		if(rukuzhuangtai==1){
			alert("不能选择已入库的包");
			btn=false;
			return btn;
		}
		if(isNullOrEmpty(rkrtiaoma)){
			alert("请输入入库人");
			btn=false;
			return btn;
		}
		if(btn){
			showMask();
	    	//弹出显示包条码
	    	$.ajax({
	            type: "POST",
	            url:"${ctx}/wupingfafang/baoruku.do?fafangkeshi="+fafangkeshi+"&rukuren="+rkrtiaoma,
				data:{"tiaomas":JSON.stringify(tiaomas)},
	            error: function(request) {
	           	 	layer.alert("网络故障，请稍后再试!");
	            },
	            success: function(data) {
	            	$("#mask").hide();
		           	if(data.status=="true"){
		 	           	layer.alert(data.msg, function(){
		 	           		//弹出条码框
		 	           		window.location.reload();
		 	              	$('input[name="key"]').prop('checked',false);
	          		    });
		           	}else{
		           		layer.msg(data.msg);
		           	}
	            }
	        });
	    }	
		
	});
});	
	//判断一个字符串是否为空
	function isNullOrEmpty(strVal){
		strVal = strVal.replace(/(^\s*)|(\s*$)/g,"");
		if(strVal == '' || strVal == null || strVal == undefined){
			return true;
		}else{
			return false;
		}
	}
	function getSelRecord(){
		var chk_value =[];
		$('input[name="key"]:checked').each(function(){
			chk_value.push($(this).val()); 
		});
		return chk_value;
	}  
	
	function getfafangzhuangtai(){
		var lishuis =[];
		$('input[name="key"]:checked').each(function(){
					if(!isNullOrEmpty($(this).attr("data2"))){
					lishuis.push($(this).attr("data2")); 
			}
		});
		return lishuis;
	} 
	function getrukuzhuangtai(){
		var lishuis =[];
		$('input[name="key"]:checked').each(function(){
					if(!isNullOrEmpty($(this).attr("data3"))){
					lishuis.push($(this).attr("data3")); 
			}
		});
		return lishuis;
	}
  	function getParameter(name) { 
		var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); 
		var r = window.location.search.substr(1).match(reg); 
		if (r!=null) return unescape(r[2]); return null;
	}
  	
  //接受条码枪的录入
	function callbackPackData(data){
		var baoArr = data;//扫码传入
		var baoTmArr = [];//页面显示
	    var wybao=[];
		var btnArr=[];
	    $('input[name="key"]').each(function(){
	    	baoTmArr.push($(this).val());
		});
	    var bao=null;
	    var baoTm=null;
	    for(var i=0;i<baoArr.length;i++){
	    	btnArr=[];
	    	bao=baoArr[i];
	    	for(var j=0;j<baoTmArr.length;j++){
	    		baoTm=baoTmArr[j];
	    		if(bao==baoTm){
	    			$('input[name="key"]').each(function(){
	    		    	if($(this).val()==bao){
	    		    		$(this).prop('checked',true);
	    		    	}
	    			});
	    		}else{
	   	    		btnArr.push(baoTm);
	    		}
	    	}
	    	if(btnArr.length==baoTmArr.length){
				wybao.push(bao);
	    	}
	    }
	    if(wybao.length!=0){
			alert("不能进行操作的包:"+wybao);
		}
	}


document.onkeydown = function (event) {
    var e = event || window.event || arguments.callee.caller.arguments[0];
    var oPlay0 = document.getElementById("audio");
    var oPlay1 = document.getElementById("audio1");
    if (e && e.keyCode == 27) { // 按 Esc
        //要做的事情
    }
    if (e && e.keyCode == 113) { // 按 F2
        //要做的事情
    }
    if (e && e.keyCode == 13) { // enter 键
        var packageTiaomaFocus = $("#packageTiaoma").is(":focus");
         if (packageTiaomaFocus == true) {
            var lan = $("#packageTiaoma").val();
             if (lan != null) {
                var flag = false;
                console.log(flag)
                $('input[name="key"]').each(function () {
                    if ($(this).val() == lan) {
                        flag = true;
                        // 清空配包的属性
                        var that = this;
                        $(that).prop('checked', true);
                        oPlay0.play();
                    }
                });

            }
            $("#packageTiaoma").val('');
        }
        //要做的事情
    }
};

  	<%--$(document).ready(function(){--%>
		<%--layer.open({--%>
			  <%--type: 2,--%>
			  <%--title: '发放/入库扫码',--%>
			  <%--maxmin: true,--%>
			  <%--shadeClose: false, //点击遮罩关闭层--%>
			  <%--area : ['800px' , '520px'],--%>
			  <%--content: '${ctx}/page/wupinfafang/fafang_barcode.jsp'--%>
			  <%--});--%>
		<%----%>
	<%--});--%>
</script>
</html>